//liuweibo add start 20120507
package com.android.matmanual;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.widget.TextView;

public class ProximityTest extends BaseActivity {

	TextView mPriximityInfo;
	WakeLock mWakeLock;
	SensorManager mSensorManager;
	SensorEventListener mSensorEventListener;
	Sensor mSensor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.proximity_test);
		setTitle(R.string.proximity_test);
		PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
		mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
				"XYTEST");
		mWakeLock.acquire();
		mPriximityInfo = (TextView) findViewById(R.id.proximity_info);
		mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

		mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
		if (mSensor == null) {
			Log.v("lifei", "############################## mSensor == null");
			mPriximityInfo.setText(getResources().getString(
					R.string.proximity_disable));
		} else {
			Log.v("lifei", "############################## mSensor == OK");
		}
		mSensorEventListener = new SensorEventListener() {

			public void onAccuracyChanged(Sensor arg0, int arg1) {
				// TODO Auto-generated method stub

			}

			public void onSensorChanged(SensorEvent arg0) {
				Log.v("lifei", "############################## onSensorChanged");
				// TODO Auto-generated method stub
				if (arg0.values[SensorManager.DATA_X] == 0) {
					mPriximityInfo
							.setText(getResources().getString(
									R.string.proximity_distance)
									+ ":"
									+ getResources().getString(
											R.string.proximity_near));

				} else if (arg0.values[SensorManager.DATA_X] > 0) {
					mPriximityInfo.setText(getResources().getString(
							R.string.proximity_distance)
							+ ":"
							+ getResources().getString(R.string.proximity_far));

				}

			}

		};

		mSensorManager.registerListener(mSensorEventListener, mSensor,
				SensorManager.SENSOR_DELAY_UI);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		int result = TestControl.GetKeyEvent(keyCode);
		if (result != TestControl.DO_NOTHING) {
			setResult(result);
			finish();
		}
		return true;
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		if (mWakeLock != null) {
			mWakeLock.release();
			mWakeLock = null;
		}
		mSensorManager.unregisterListener(mSensorEventListener);

		super.onDestroy();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		finish();
		super.onStop();
	}

	@Override
	public void onAttachedToWindow() {
		// TODO Auto-generated method stub
		super.onAttachedToWindow();
		this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
	}
}
// liuweibo end
